package com.tjbank.cssys.web.service.impl;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tjbank.cssys.repository.CkCreditOutCompanyDataRelationMapper;
import com.tjbank.cssys.web.service.CkCreditOutCompanyDataRelationService;
import com.tjbank.cssys.web.service.CkUserService;
import com.tjbank.cssys.web.util.GetUserCodeFromToken;
import com.tjbank.cssys.web.util.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CkCreditOutCompanyDataRelationServiceImpl implements CkCreditOutCompanyDataRelationService {

    private static Logger logger = LoggerFactory.getLogger(CkCreditOutCompanyDataRelationServiceImpl.class);

    @Autowired
    private CkCreditOutCompanyDataRelationMapper ckCreditOutCompanyDataRelationMapper;
    @Autowired
    private CkUserService ckUserService;

    @Override
    public PageInfo listData(HttpServletRequest request, Map<String, String> params) {
        logger.info("委外公司对账列表数据查询---start,params=" + JSON.toJSONString(params));

        String userCode = null;
        try {
            userCode = GetUserCodeFromToken.getUserCode(request);
        } catch (Exception e) {

        }
        HashMap<String, Object> userBranchAndSub = ckUserService.getUserBranchAndSub(userCode);
        int page = Integer.parseInt(params.getOrDefault("page", "1").toString());
        int pageSize = Integer.parseInt(params.getOrDefault("pageSize", "10").toString());


        if (null != userBranchAndSub && null != userBranchAndSub.get("branch")){
            Map<String ,String > result= (Map<String, String>) userBranchAndSub.get("branch");
            params.put("bankcode",result.get("CODE"));
        }
            PageHelper.startPage(page, pageSize);
        List<Map<String, Object>> datas = ckCreditOutCompanyDataRelationMapper.selectList(params);
        //List<Map<String,Object>> ckCreditRecoverMonies = ckCreditOutCompanyDataRelationMapper.selectRePeoples(params);

        PageInfo result = new PageInfo(datas);
        result.setList(MapUtils.formatHumpNameForList(result.getList()));
        logger.info("委外公司对账列表数据查询---end,PageInfo=" + JSON.toJSONString(result));
        return result;
    }

    @Override
    public void updateBrokerageAndRateById(Map<String, Object> params) {
        logger.info("委外公司对账---start,params=" + JSON.toJSONString(params));
        int total = ckCreditOutCompanyDataRelationMapper.updateBrokerageAndRateById(params);
        logger.info("委外公司对账---end,total=" + JSON.toJSONString(total));
    }
}
